package com.example.springbootfoodsafety.contrlloer;


import com.example.springbootfoodsafety.entity.User;
import com.example.springbootfoodsafety.po.Article;
import com.example.springbootfoodsafety.po.Friends;
import com.example.springbootfoodsafety.serviceimpl.UserServiceImpl;
import com.example.springbootfoodsafety.util.CodeMsg;
import com.example.springbootfoodsafety.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserContrlloer {

    @Autowired
    UserServiceImpl service;

    @PostMapping("/getUserArticle")
    public Result getUserArticle(@RequestBody Object o){
        String str=o.toString();
        for(int i=str.length()-1;i>=0;i--)
        {
            if(str.charAt(i)!='}')
            {
                str=str.substring(4,i+1);
                break;
            }
        }
        Integer id=Integer.parseInt(str);
        System.out.println(id);
        List<Article> list=service.getUserArticle(id);
        if(list!=null)
        return Result.success(list);
        else{
            return Result.error(new CodeMsg(400,"没有"));
        }
    }
    @PostMapping("/modifyUser")
    public Result modifyUser(@RequestBody User user){
        System.out.println(user);
        boolean flag=service.modifyUser(user);
        return Result.success(flag);
    }

@PostMapping("/getUserFriends")
    public Result getUserFriends(Integer id){
        List<User> list=service.getUserFriends(id);
        System.out.println(list);
        return Result.success(list);
    }

    @PostMapping("/addFriends")
    public Result addFriends(@RequestBody Friends friends) {
        System.out.println(friends);
        if(friends.getMasterId()==-1 || friends.getFriendId()==-1)
        {
            return Result.error(new CodeMsg(200,"error"));
        }
        return Result.success(service.addFriends(friends.getMasterId(),friends.getFriendId()));
    }


}
